using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;
using TemplateWeb.DAO;
using TemplateWeb.Helpers;

namespace TemplateWeb.DTO
{
	/// <summary>
	/// Data transfer object which is associated with HopThu table in database
	/// </summary>
	public class HopThuDTO
	{
		public int ID { get; set; }
		public string TieuDe { get; set; }
		public string NoiDung { get; set; }
		public string HoTen { get; set; }
		public string EmailNguoiHoi { get; set; }
		public DateTime NgayHoi { get; set; }
		public bool DaPhanHoi { get; set; }
        public List<PhanHoiDTO> PhanHoi { get; set; }

		public static HopThuDTO Translate(DataRow row)
		{
			var dto = new HopThuDTO
			{
				ID = int.Parse(row["ID"].ToString()),
				TieuDe = row["TieuDe"].ToString(),
				NoiDung = row["NoiDung"].ToString(),
				HoTen = row["HoTen"].ToString(),
				EmailNguoiHoi = row["EmailNguoiHoi"].ToString(),
				NgayHoi = DateTime.Parse(row["NgayHoi"].ToString()),
				DaPhanHoi = bool.Parse(row["DaPhanHoi"].ToString()),
			};

            dto.PhanHoi = new List<PhanHoiDTO>();

            // Select phan hoi
            var rpt = new PhanHoiDAO(WebConfiguration.DbConnectionString);
            var objDS = rpt.SelectByIDHopThu(dto.ID);
            if (objDS != null && objDS.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow datarow in objDS.Tables[0].Rows)
                {
                    dto.PhanHoi.Add(PhanHoiDTO.Translate(datarow));
                }
            }

			return dto;
		}
	}
}